//
//  SecondViewController.m
//  QQViiew
//
//  Created by liuchengming on 16/9/28.
//  Copyright © 2016年 liuchengming. All rights reserved.
//

#import "SecondViewController.h"
#import "QQTableViewCell.h"
#import "Person.h"

@interface SecondViewController ()
{
    NSArray * data;
    
}

@end

@implementation SecondViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    

    self.tableView.rowHeight = UITableViewAutomaticDimension;
    
    self.tableView.estimatedRowHeight = 44;
    
    
    
    
    
    NSString * path = @"/Users/liuchengming/Desktop/002.vcf";
    NSString * str = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
    NSArray * arr = [str componentsSeparatedByString:@"\n"];
    
    //NSLog(@"%@",arr);
    
    Person * per ;
    
    NSMutableArray * item = [NSMutableArray new] ;
    
    
    
    //解析数据
    for (NSString * s in arr)
    {
        if([s hasPrefix:@"BEGIN"])
        {
            per = [Person new];
        }
        else if([s hasPrefix:@"TEL"])
        {
            NSArray * arr = [s componentsSeparatedByString:@":"];
            
            per.tel = arr.lastObject;
        }else if([s hasPrefix:@"FN"])
        {
            NSArray * arrs = [s componentsSeparatedByString:@":"];
            NSString * names = arrs.lastObject;
            names = [names stringByReplacingOccurrencesOfString:@"=" withString:@"%"];
            names = [names stringByRemovingPercentEncoding];
            
            
            per.name = names ;
            //NSLog(@"%@",names);
        }else if([s hasPrefix:@"END"])
        {
            [item addObject:per];
        }
    }
    data = item;
    
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return data.count;
}

- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString * str = [NSString stringWithFormat:@"%d",(int)indexPath.row + 1];
    
    QQTableViewCell * cell  = [tableView dequeueReusableCellWithIdentifier:@"style1" forIndexPath:indexPath];
    
    Person * p = data[indexPath.row];
    
    cell.logo.image =  [UIImage imageNamed:str];
    
    if(cell.logo.image == nil)
    {
        cell.logo.image = [UIImage imageNamed:@"1"];
    }
    cell.name.text = p.name;
    cell.test.text = p.tel;
    
    cell.time.text = @"12:59";
    
    if (indexPath.row %2 == 0)
    {
        cell.backgroundColor = [UIColor grayColor];
    }else
    {
        cell.backgroundColor = [UIColor darkGrayColor];
    }
    
    //NSLog(@"%@",arr[i]);
    //NSLog(@"%d",i);
    return cell;
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
